<?php
class order_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert("dtb_orders", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ID,$params){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where('OrderID', $ID);
        $this->db->update("dtb_orders", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function get($ID=""){
        $query = $this->db->query("
            SELECT dtb_orders.*
                ,dtb_customer.CustomerName,Phone
                ,dtb_product.ProductName,Alias
            FROM dtb_orders
                LEFT JOIN dtb_product ON ( dtb_product.ProductID = dtb_orders.ProductID )
                LEFT JOIN dtb_customer ON ( dtb_orders.CustomerID = dtb_customer.CustomerID )
            WHERE dtb_orders.OrderID = '$ID'
        ");
        return $query->result_array();
    }
    function binding(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `dtb_orders`.*
                ,`dtb_orders`.`OrderID` as 'ID'
                ,dtb_customer.CustomerName
                ,dtb_product.ProductName
            FROM `dtb_orders`
                LEFT JOIN `dtb_customer` ON (dtb_orders.CustomerID=dtb_customer.CustomerID)
                LEFT JOIN `dtb_product` ON (dtb_orders.ProductID=dtb_product.ProductID)
            ";
        $strWhere="
            WHERE  true
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `dtb_orders`.`Delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `dtb_orders`.`Delete` IS NOT NULL";
        }
        switch ($_SESSION["ADP"][CCTRL]["status"]){
            case 0:
                
                break;
            case 1:
                $strWhere.=" AND `dtb_orders`.`Status` IN ('New','Confirmed','Shipping')";
                break;
            case 2:
                $strWhere.=" AND `dtb_orders`.`Status` = 'Finished'";
                break;
            case 3:
                $strWhere.=" AND `dtb_orders`.`Status` = 'Canceled'";
                break;
        }
        $strOrderBy="Order By dtb_orders.`Insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>true,
            "fields"=>array(
                "ID"=>"dtb_orders.OrderID",
                "CustomerID"=>"dtb_orders.CustomerID",
                "CustomerName"=>"dtb_customer.CustomerName",
                "Order"=>"dtb_orders.OrderID",
                "OrderID"=>"dtb_orders.OrderID",
                "Status"=>"dtb_orders.Status",
                "Insert"=>"dtb_orders.Insert",
                "Update"=>"dtb_orders.Update",
                "ShipPhone"=>"OrderDetail",
            ),
            "datefields"=>array("OrderDate","Insert","Update","Delete")
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    
}
?>
